package com.yogee.topseller.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.SharedPreferencesUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.base.HttpActivity;
import com.yogee.topseller.adapter.FlashRvAdapter;
import com.yogee.topseller.bean.FastQueryBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 闪搜
 * Created by zwj on 2017/12/9.
 */

public class FlashActivity extends HttpActivity {

    @BindView(R.id.flash_sousuo_num_tv)
    TextView flashSousuoNumTv;
    @BindView(R.id.flash_liulan_num_tv)
    TextView flashLiulanNumTv;
    @BindView(R.id.flash_goumai_num_tv)
    TextView flashGoumaiNumTv;
    @BindView(R.id.flash_rv)
    RecyclerView flashRv;
    private FlashRvAdapter adapter;
    private String tel;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_flash;
    }

    @Override
    protected void initView() {
        adapter = new FlashRvAdapter(this);
        flashRv.setLayoutManager(new LinearLayoutManager(this));
        flashRv.setHasFixedSize(true);
        flashRv.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fastQuery(AppUtil.getUserId(this));
    }

    @OnClick({R.id.flash_pipei_tv,R.id.flash_details_back_iv})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.flash_details_back_iv:
                finish();
                break;
            case R.id.flash_pipei_tv:
                if (!tel.equals("")){
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + tel);
                    intent.setData(data);
                    startActivity(intent);
                }
                break;
        }

    }

    private void fastQuery(String userId) {
        HttpManager.getInstance().fastQuery(userId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<FastQueryBean>() {
            @Override
            public void onNext(FastQueryBean o) {
                if(o.getQueryList()==null||o.getQueryList().size()==0){
                    SharedPreferencesUtils.put(FlashActivity.this,SharedPreferencesUtils.HAVE_FLASH,false);
                }else{
                    SharedPreferencesUtils.put(FlashActivity.this,SharedPreferencesUtils.HAVE_FLASH,true);
                }
                adapter = new FlashRvAdapter(FlashActivity.this);
                flashRv.setAdapter(adapter);
                adapter.addData(o.getQueryList(),o.getTel());
                flashSousuoNumTv.setText(o.getQueryNum());
                flashLiulanNumTv.setText(o.getBrowseNum());
                flashGoumaiNumTv.setText(o.getBuyNum());
                tel = o.getTel();
            }
        }, this));
    }

}
